/*
#**************************************************************************
#  openSIS is a student information system for public and non-public 
#  schools from Open Solutions for Education, Inc. web: www.os4ed.com
#
#  openSIS is  web-based, open source, and comes packed with features that 
#  include student demographic info, scheduling, grade book, attendance, 
#  report cards, eligibility, transcripts, parent portal, 
#  student portal and more.   
#
#  Visit the openSIS web site at http://www.opensis.com to learn more.
#  If you have question regarding this system or the license, please send 
#  an email to info@os4ed.com.
#
#  This program is released under the terms of the GNU General Public License  
#  as  published by the Free Software Foundation, version 2 of the License. 
#  See license.txt.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#**************************************************************************** 
*/
using System;
using System.Web;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;
using System.Collections.Specialized;
using System.Data;
using calendar;
using calendar.Data;
using calendar.Controls;
using calendar.Security;
using calendar.Configuration;
//using _OSISInstallationBusinessLayer;
//using _OSISCalenderDetailsBusinessLayer;
using OpenSIS.OpenSISWCFServiceReference;



//End Using Statements

namespace calendar.index{ //Namespace @1-FD158137

//Page Data Class @1-3FCDCDA8
public class PageItem
{
    public NameValueCollection errors = new NameValueCollection();
    public static PageItem CreateFromHttpRequest()
    {
        PageItem item = new PageItem();
        return item;
    }

    public FieldBase this[string fieldName]{
        get
        {
            switch(fieldName)
            {
                default:
                    throw (new ArgumentOutOfRangeException());
            }
        }
        set
        {
            switch(fieldName){
                default:
                    throw (new ArgumentOutOfRangeException());
        }
        }
    }

    public PageItem()
    {
    }
}
//End Page Data Class

//Page Data Provider Class @1-50FE4D41
public class PageDataProvider
{
//End Page Data Provider Class

//Page Data Provider Class Constructor @1-9A44B219
    public PageDataProvider()
    {
    }
//End Page Data Provider Class Constructor

//Page Data Provider Class GetResultSet Method @1-052161C6
    public void FillItem(PageItem item)
    {
//End Page Data Provider Class GetResultSet Method

//Page Data Provider Class GetResultSet Method tail @1-FCB6E20C
    }
//End Page Data Provider Class GetResultSet Method tail

//Page Data Provider class Tail @1-FCB6E20C
}
//End Page Data Provider class Tail

//Calendar cal_month Item Class @5-77538276
public class cal_monthItem:ICalendarDataItem
{
   


    public DateField MonthDate;
    public DateField DayOfWeek;
    public DateField DayNumber;
    public object DayNumberHref;
    public LinkParameterCollection DayNumberHrefParameters;
    public TextField add_event;
    public object add_eventHref;
    public LinkParameterCollection add_eventHrefParameters;
    public TextField category_image;
    public DateField EventTime;
    public DateField EventTimeEnd;
    public TextField EventDescription;

    //public TextField Eventindex;

    public TextField EventDescriptiontooltip;
    //added by abhishek on 5th june
    public TextField EventNotes;
     public DateField EventDate;
   
    //added by abhishek on 5th june
    public object EventDescriptionHref;
    public LinkParameterCollection EventDescriptionHrefParameters;
    public TextField go_week;
    public object go_weekHref;
    public LinkParameterCollection go_weekHrefParameters;
    public TextField YearIcon;
    public object YearIconHref;
    public LinkParameterCollection YearIconHrefParameters;
    public TextField MonthIcon;
    public object MonthIconHref;
    public LinkParameterCollection MonthIconHrefParameters;
    public TextField WeekIcon;
    public object WeekIconHref;
    public LinkParameterCollection WeekIconHrefParameters;

    //added by abhishek on 19th august
    public TextField Eventtype;
   //added by abhishek on 19th august


    public NameValueCollection errors=new NameValueCollection();
    public cal_monthItem()
    {
        //16th july

       // InstallationBL InstallBL = new InstallationBL();

        OpenSISWCFServiceInstallationClient InstallBL = new OpenSISWCFServiceInstallationClient();

       // DataTable dtDateFormat = InstallBL.getdateformat();
        //string dateformat = dtDateFormat.Rows[0][0].ToString();
        string dateformat = "MM/dd/yyyy";
        // EventDate = new DateField("dd MMMM yyyy", null);

        EventDate = new DateField(dateformat, null);
        //16th july

        MonthDate=new DateField("MMMM yyyy", null);
        DayOfWeek=new DateField("dddd", null);
        DayNumber = new DateField("%d",null);
        DayNumberHrefParameters = new LinkParameterCollection();
        add_event = new TextField("",null);
        add_eventHrefParameters = new LinkParameterCollection();
        category_image=new TextField("", null);
        EventTime=new DateField("t", null);
        EventTimeEnd=new DateField("t", null);
        EventDescription = new TextField("",null);

        EventDescriptiontooltip = new TextField("", null);


        Eventtype = new TextField("", null);
        //Eventindex = new TextField("", null);
        

        EventDescriptionHrefParameters = new LinkParameterCollection();
        go_week = new TextField("",null);
        go_weekHrefParameters = new LinkParameterCollection();
        YearIcon = new TextField("",null);
        YearIconHrefParameters = new LinkParameterCollection();
        MonthIcon = new TextField("",null);
        MonthIconHrefParameters = new LinkParameterCollection();
        WeekIcon = new TextField("",null);
        WeekIconHrefParameters = new LinkParameterCollection();

        //added by abhishek on 5th june
        EventNotes = new TextField("", null);

        InstallBL.Close();
    }
    public FieldBase this[string fieldName]{
        get{
            switch(fieldName){
                case "MonthDate":
                    return this.MonthDate;
                case "DayOfWeek":
                    return this.DayOfWeek;
                case "DayNumber":
                    return this.DayNumber;
                case "add_event":
                    return this.add_event;
                case "category_image":
                    return this.category_image;
                case "EventTime":
                    return this.EventTime;
                case "EventTimeEnd":
                    return this.EventTimeEnd;
                case "EventDescription":
                    return this.EventDescription;

                case "EventDescriptiontooltip":
                    return this.EventDescriptiontooltip;

                case "Eventtype":
                    return this.Eventtype;

                //case "Eventindex":
                //    return this.Eventindex;

                case "go_week":
                    return this.go_week;
                case "YearIcon":
                    return this.YearIcon;
                case "MonthIcon":
                    return this.MonthIcon;
                case "WeekIcon":
                    return this.WeekIcon;
                case "EventDateCalendarField":
                    return this.EventDateCalendarField;
                case "EventTimeCalendarField":
                    return this.EventTimeCalendarField;


                case "EventNotes":
                    return this.EventNotes;
                case "EventDate":
                    return this.EventDate;


                default:
                    throw (new ArgumentOutOfRangeException());
            }
        }
        set{
            switch(fieldName){
                case "MonthDate":
                    this.MonthDate = (DateField)value;
                    break;
                case "DayOfWeek":
                    this.DayOfWeek = (DateField)value;
                    break;
                case "DayNumber":
                    this.DayNumber = (DateField)value;
                    break;
                case "add_event":
                    this.add_event = (TextField)value;
                    break;
                case "category_image":
                    this.category_image = (TextField)value;
                    break;
                case "EventTime":
                    this.EventTime = (DateField)value;
                    break;
                case "EventTimeEnd":
                    this.EventTimeEnd = (DateField)value;
                    break;
                case "EventDescription":
                    this.EventDescription = (TextField)value;
                    break;



                case "EventDescriptiontooltip":
                    this.EventDescriptiontooltip = (TextField)value;
                    break;

                case "Eventtype":
                    this.Eventtype = (TextField)value;
                    break;


                //case "Eventindex":
                //    this.Eventindex = (TextField)value;
                //    break;


                case "EventNotes":
                    this.EventNotes = (TextField)value;
                    break;

                case "EventDate":
                    this.EventDate = (DateField)value;
                    break;



                case "go_week":
                    this.go_week = (TextField)value;
                    break;
                case "YearIcon":
                    this.YearIcon = (TextField)value;
                    break;
                case "MonthIcon":
                    this.MonthIcon = (TextField)value;
                    break;
                case "WeekIcon":
                    this.WeekIcon = (TextField)value;
                    break;
                case "EventDateCalendarField":
                    this.EventDateCalendarField = (DateField)value;
                    break;
                case "EventTimeCalendarField":
                    this.EventTimeCalendarField = (DateField)value;
                    break;
                default:
                    throw (new ArgumentOutOfRangeException());
            }
        }
    }

    protected DateField m_eventDateCalendarField = new DateField("", null);
    protected DateField m_eventTimeCalendarField = new DateField("", null);
    protected bool m_IsEventTimeExists = true;
    public bool IsEventTimeExists{
        get{
            return this.m_IsEventTimeExists;
        }
        set{
            this.m_IsEventTimeExists = value;
        }
    }
    public DateField EventDateCalendarField{
        get{
            return this.m_eventDateCalendarField;
        }
        set{
            this.m_eventDateCalendarField = (DateField)value;
        }
    }
    public DateField EventTimeCalendarField{
        get{
            return this.m_eventTimeCalendarField;
        }
        set{
            this.m_eventTimeCalendarField = (DateField)value;
        }
    }
}

public class cal_monthDataProvider:GridDataProviderBase
{


    public enum SortFields {Default}
    private string[] SortFieldsNames=new string[]{"event_time, event_time_end"};
    private string[] SortFieldsNamesDesc=new string[]{"event_time, event_time_end"};
    public SortFields SortField=SortFields.Default;
    public SortDirections SortDir=SortDirections.Asc;
    public IntegerParameter Sescategory;


    public cal_monthDataProvider()
    {
        Select = new TableCommand("SELECT event_id, event_title, event_date,event_desc, event_time, \n" +
         "event_time_end, \n" +
         "event_is_public,event_type \n" +
         "FROM events \n" +
         "{SQL_Where} {SQL_OrderBy}", new string[] { "" }, Settings.calendarDataAccessObject);
    }

    public cal_monthItem[] GetResultSet(FormSupportedOperations ops,int Calendarid)
    {

		string CurrentDateStr = System.Web.HttpContext.Current.Session["CurrentDate"].ToString();
		DateTime FirstDate = CommonFunctions.CCParseDate(CurrentDateStr + "-01", "yyyy-MM-dd");
		DateTime LastDate = FirstDate.AddMonths(1).AddDays(-1);

	    ((TableCommand)Select).Operation = "And";
		DataAccessObject DBcalendar = Settings.calendarDataAccessObject;
		string filter = CommonFunctions.AddReadFilter("");
		((TableCommand)Select).Where = "event_date >=" + DBcalendar.ToSql(FirstDate.ToString("yyyy-MM-dd"), FieldType.Date) +
				" AND event_date <=" + DBcalendar.ToSql(LastDate.ToString("yyyy-MM-dd"), FieldType.Date) +
                (filter.Length > 0 ? " AND " + filter : "") + " AND Sch_ID =" + System.Web.HttpContext.Current.Session["SchoolID"] + " AND event_parent_id =" + Calendarid;
        Select.Parameters.Clear();
       
        ((TableCommand)Select).AddParameter("", Sescategory, "", "", Condition.Equal, false);
       
       Select.OrderBy = (SortDir==SortDirections.Asc?SortFieldsNames[(int)SortField]:SortFieldsNamesDesc[(int)SortField]).Trim();
        int tableIndex = 0;
        Exception E=null;

        DataSet ds=null;
        cal_monthItem[] result = new cal_monthItem[0];
        if (ops.AllowRead) {
            try{
                ds=ExecuteSelect();
            }catch(Exception e){
                E=e;}
            finally
            {
                if(E!=null) throw(E);
            }
            DataRowCollection dr=ds.Tables[tableIndex].Rows;
            result = new cal_monthItem[dr.Count];
            for(int i=0;i<dr.Count;i++)
            {
                cal_monthItem item=new cal_monthItem();
                item.DayNumberHref = "day.aspx";
                item.add_eventHref = "events.aspx";
                item.EventTime.SetValue(dr[i]["event_time"],"HH\\:mm\\:ss");
                item.EventTimeEnd.SetValue(dr[i]["event_time_end"],"HH\\:mm\\:ss");

                if (dr[i]["event_title"].ToString().Length > 9)
                {

                    item.EventDescription.SetValue(dr[i]["event_title"].ToString().Substring(0, 10) + "...", "");
                }
                else
                {
                    item.EventDescription.SetValue(dr[i]["event_title"], "");
                
                }

                item.EventNotes.SetValue(dr[i]["event_desc"], "");
                item.EventDate.SetValue(dr[i]["event_date"],"");
                item.EventDescriptionHref = "event_view.aspx";
                item.EventDescriptionHrefParameters.Add("event_id",System.Web.HttpUtility.UrlEncode(dr[i]["event_id"].ToString()));
                item.go_weekHref = "week.aspx";
                item.YearIconHref = "year.aspx";
                item.MonthIconHref = "index.aspx";
                item.WeekIconHref = "week.aspx";
                item.EventDateCalendarField.SetValue(dr[i]["event_date"],"yyyy-MM-dd");
                item.EventTimeCalendarField.SetValue(dr[i]["event_time"],"HH\\:mm\\:ss");

                //added
                item.EventDescriptiontooltip.SetValue(dr[i]["event_title"], "");

                string eventtype = dr[i]["event_type"].ToString().Trim();

                item.Eventtype.SetValue(eventtype, "");



                result[i]=item;


            }
            _isEmpty = dr.Count == 0;
        }
        return result; 
    }
}
}
